/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-07 09:26:03
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-18 18:37:58
 * @FilePath: /yt-devops/lib/pages/site/site_overview/site_overview_model.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:yt_devops/resources/str_res_keys.dart';

enum FlowDirection {
  ins(-1),
  outs(1),
  stops(0);

  final int value;
  const FlowDirection(this.value);
}

enum EnergyDeviceType {
  pv(1),
  storage(2),
  charge(3),
  load(4),
  grid(5),
  diesel(6),
  electricPrice(7),
  userPower(99);

  final dynamic value;
  const EnergyDeviceType(this.value);

  String get label {
    switch (value) {
      case 1:
        return ResKey.siteOverviewPhotovoltaic.tr;
      case 2:
        return ResKey.energyStorage.tr;
      case 3:
        return ResKey.devicechargingPile.tr;
      case 4:
        return ResKey.deviceLoad.tr;
      case 5:
        return ResKey.siteOverviewPower.tr;
      case 99:
        return ResKey.deviceLoad.tr;
      default:
        return ResKey.unknown.tr;
    }
  }

  static EnergyDeviceType? getType(dynamic value) {
    return EnergyDeviceType.values.firstWhereOrNull(
      (element) => element.value == value,
    );
  }
}

enum NamePositionType { top, bottom }

/// 能源流动Model
class EnergyFlowModel {
  int? type;

  /// 流动方向
  FlowDirection? direction;

  /// 功率
  num? p;

  String get getValueStr => p == null ? '--' : p.toString();

  String get unit => 'kW';

  EnergyFlowModel({
    this.type,
    this.direction,
    this.p,
  });

  factory EnergyFlowModel.fromJson(Map<String, dynamic> json) =>
      EnergyFlowModel(
        type: json["type"] ?? 0,
        direction: FlowDirection.values.firstWhere(
          (element) => element.value == json["direction"],
          orElse: () => FlowDirection.stops,
        ),
        p: json["p"],
      );
}

class CircleModel {
  final String name;
  final ImageProvider bgImage;
  final ImageProvider image;
  final String energyValue;
  NamePositionType namePosition;
  final Widget? nameWidget;
  final EnergyFlowModel? energyFlowModel;
  final Function()? onTap;

  CircleModel(
      {required this.name,
      this.nameWidget,
      required this.bgImage,
      required this.image,
      required this.energyValue,
      required this.energyFlowModel,
      this.namePosition = NamePositionType.bottom,
      this.onTap});
}

/// 站点下设备数量
class DeviceDataModel {
  final int totalCount;

  final int onlineCount;

  DeviceDataModel({
    this.onlineCount = 0,
    this.totalCount = 0,
  });

  factory DeviceDataModel.fromJson(Map<String, dynamic> json) =>
      DeviceDataModel(
        totalCount: json["totalCount"] ?? 0,
        onlineCount: json["onlineCount"] ?? 0,
      );
}

/// 统计数据项Model
/// 用于展示统计数据项
class StatisticsItemModel {
  // 数值
  final double? value;

  // 单位
  final String? unit;

  // 标题
  final String? name;

  double get getValue {
    // if (unit == 'kWh' && (value ?? 0) >= 10000) {
    //   return double.parse((value! / 10000).toStringAsFixed(2));
    // } else {
    return value!;
    // }
  }

  String get getUnitText {
    // if (unit == 'kWh' && (value ?? 0) >= 10000) {
    //   return 'mWh';
    // } else {
    return unit ?? '';
    // }
  }

  const StatisticsItemModel({
    this.value = 0.0,
    this.unit,
    this.name,
  });
}

class SiteOverviewWeather {
  SiteOverviewWeather({
    this.daytemp,
    this.humidity,
    this.nighttemp,
    this.temperature,
    this.weather,
    this.winddirection,
  });

  ///白天温度
  String? daytemp;

  ///湿度
  String? humidity;

  ///晚上温度
  String? nighttemp;

  ///实时气温，单位：摄氏度
  String? temperature;

  ///天气现象（汉字描述）
  String? weather;

  ///风向描述
  String? winddirection;

  factory SiteOverviewWeather.fromJson(Map<String, dynamic> json) =>
      SiteOverviewWeather(
        daytemp: json["daytemp"] ?? '',
        humidity: json["humidity"] ?? '',
        nighttemp: json["nighttemp"] ?? '',
        temperature: json["temperature"] ?? '',
        weather: json["weather"] ?? '',
        winddirection: json["winddirection"] ?? '',
      );

  Map<String, dynamic> toJson() => {
        "daytemp": daytemp,
        "humidity": humidity,
        "nighttemp": nighttemp,
        "temperature": temperature,
        "weather": weather,
        "winddirection": winddirection,
      };
}
